package com.haoshuang.zhouzhoubang.utils

import java.text.DecimalFormat
import java.util.Calendar

/**
 * 日期时间工具类
 */
object DateTools {
    /**
     * 返回一月有几天
     * @param year Int
     * @param month Int
     * @return Int
     */
    fun getMonthOfDay(year: Int, month: Int): Int {
        var day = 0
        day = if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
            29
        } else {
            28
        }
        when (month) {
            1, 3, 5, 7, 8, 10, 12 -> return 31
            4, 6, 9, 11 -> return 30
            2 -> return day
        }
        return 0
    }

    /**
     * 返回当前月份1号位于周几
     *
     * @param year  年份
     * @param month 月份，传入系统获取的，不需要正常的
     * @return 日：1      一：2     二：3     三：4     四：5     五：6     六：7
     */
    fun getFirstDayWeek(year: Int, month: Int): Int {

        val calendar: Calendar = Calendar.getInstance()
        //设置某一年
        calendar.set(year, month - 1, 1)
        return calendar.get(Calendar.DAY_OF_WEEK)

    }

    fun millisecondsConvertToHMS(milliseconds: Long): String {
        val hoursStr: String
        val minutesStr: String
        val secondsStr: String
        val hour = milliseconds / 1000 / 3600
        hoursStr = if (hour < 10) {
            "0$hour"
        } else {
            hour.toString() + ""
        }
        val minute = milliseconds / 1000 % 3600 / 60
        minutesStr = if (minute < 10) {
            "0$minute"
        } else {
            minute.toString() + ""
        }
        val second = milliseconds / 1000 % 60
        secondsStr = if (second < 10) {
            "0$second"
        } else {
            second.toString() + ""
        }
        return "$hoursStr:$minutesStr:$secondsStr"
    }

    fun formatDateTime(countdown: Long): String {
        val sb = StringBuilder()
        val mss = countdown / 1000
        val days = mss / (60 * 60 * 24)
        val hours = mss % (60 * 60 * 24) / (60 * 60)
        val minutes = mss % (60 * 60) / 60
        val seconds = mss % 60
        val format = DecimalFormat("00")

        if (days > 0 || hours > 0) {
            sb.append(format.format(hours)).append(":").append(format.format(minutes)).append(":")
                .append(format.format(seconds))
        } else {
            sb.append("00").append(":").append(format.format(minutes)).append(":")
                .append(format.format(seconds))
        }
        return sb.toString()
    }

}